//
//  IWNewFutureController.m
//  SINA
//
//  Created by 王相超 on 16/3/29.
//  Copyright © 2016年 chuanzhi. All rights reserved.
//

#import "IWNewFutureController.h"
#import "IWTabBarController.h"
#import "IWAccoutTool.h"
#import "IWOAuthViewController.h"
#import "IWAccount.h"
#import "AppDelegate.h"
#import "IWWindowSwitch.h"

@interface IWNewFutureController ()<UIScrollViewDelegate>

@property(weak,nonatomic)UIPageControl *pageControl;

@end

@implementation IWNewFutureController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.pagingEnabled=YES;
    scrollView.delegate = self;
    
    NSInteger count = 4;
    
    for (int i = 0; i < count; i++) {
        
        UIImageView *imageView = [[UIImageView alloc] init];
        
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"new_feature_%d",i+1]];
        imageView.size = scrollView.size;
        imageView.x = i * scrollView.width;
        
        [scrollView addSubview:imageView];
        
        if (i == count - 1) {
         
            [self setupLastPageWithView:imageView];
        }
    }
    
    [scrollView setContentSize:CGSizeMake(scrollView.width * count, 0)];
    
    [self.view addSubview:scrollView];
    
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    
    pageControl.numberOfPages = count;
    
    pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
    pageControl.pageIndicatorTintColor = [UIColor grayColor];
    
    pageControl.centerX = self.view.width / 2;
    pageControl.y = self.view.height - 100;
    
    self.pageControl = pageControl;
    
    [self.view addSubview:pageControl];
    
    
}

-(void)setupLastPageWithView:(UIImageView *)imageView {
    
    imageView.userInteractionEnabled = YES;
    
    
    UIButton *enterBtn = [[UIButton alloc] init];
    
    [enterBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal];
    [enterBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
    
    enterBtn.size = enterBtn.currentBackgroundImage.size;
    enterBtn.centerX = imageView.width * 0.5;
    enterBtn.y = imageView.height - 150;
    
    [enterBtn setTitle:@"进入" forState:UIControlStateNormal];
    
    [enterBtn addTarget:self action:@selector(enterBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [imageView addSubview:enterBtn];
    
    UIButton *shareBtn = [[UIButton alloc] init];
    
    [shareBtn setTitle:@"分享到首页" forState:UIControlStateNormal];
    [shareBtn setImage:[UIImage imageNamed:@"new_feature_share_false"] forState:UIControlStateNormal];
    [shareBtn setImage:[UIImage imageNamed:@"new_feature_share_true"] forState:UIControlStateSelected];
    [shareBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    
    [shareBtn addTarget:self action:@selector(shareBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [shareBtn sizeToFit];
    
    shareBtn.centerX = enterBtn.centerX;
    shareBtn.y = enterBtn.y - 40;
    
    [imageView addSubview:shareBtn];
}

-(void)shareBtnClick:(UIButton *)sender{
    
    sender.selected = !sender.selected;
}

-(void)enterBtnClick:(UIButton *)sender {
    
    
    [IWWindowSwitch IWWindowSwitch];
    
     NSString *currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
    
    [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:@"key_ShortVersion"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    double page = scrollView.contentOffset.x / scrollView.width;
    
    self.pageControl.currentPage = (int)(page + 0.5);
}

@end
